<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>活动管理</title>
	<meta name="decorator" content="default"/>
	<%@include file="/WEB-INF/views/include/treeview.jsp" %>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
			
			
			var selectedNodeIds = '';
			var setting = {check:{enable:true,nocheckInherit:true},view:{selectedMulti:false},
					data:{simpleData:{enable:true}},
					callback:{
						beforeClick:function(id, node){
						tree.checkNode(node, !node.checked, true, true);
						return false;
						},
						onCheck: function (event, treeId, treeNode) {
						    //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);					
							selectedNodeIds = '';
							var nodes = tree.getCheckedNodes();
							for(var index in nodes) {
								var node = nodes[index];
								var nodeChildren = node.children;
								if(!nodeChildren || nodeChildren.length == 0){
									selectedNodeIds += ("," + node.id);
								}
							}
							$("#channelIds").val(selectedNodeIds);
						}
					}
				};
			
			// 用户-菜单
			var zNodes=[
					<c:forEach items="${channelList}" var="menu">{id:"${menu.id}", pId:"${not empty menu.parent.id?menu.parent.id:0}", name:"${not empty menu.parent.id?menu.name:'渠道列表'}"},
		            </c:forEach>];
			// 初始化树结构
			var tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
			// 不选择父节点
			tree.setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
 			// 默认选择节点
			var ids = "${event.channelIds}".split(",");
			for(var i=0; i<ids.length; i++) {
				var node = tree.getNodeByParam("id", ids[i]);
				try{tree.checkNode(node, true, false);}catch(e){}
			} 
			// 默认展开全部节点
			tree.expandAll(true);
			
			
		});
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
		
		
		function changetype(type){
			if(type == "804"){
				$("#eventFlowListGroup").css('display','block');
				$("#eventLotteryListGroup").css('display','none');
				$("#eventPrizeListGroup").css('display','none');
				$("#enrollLimitGroup").css('display','none');
				$("#remarksGroup").css('display','none');
				$("#titleGroup").css('display','block');
				
				$("#interactStartDateGroup").css('display','none');
				$("#interactEndDateGroup").css('display','none');
				$("#briefGroup").css('display','block');
				$("#descriptionGroup").css('display','block');
				$("#moreImagesGroup").css('display','none');
				$("#enrollLimitGroup").css('display','none');
				
				
			}else if(type == "805"){
				$("#eventFlowListGroup").css('display','block');
				$("#eventLotteryListGroup").css('display','none');
				$("#eventPrizeListGroup").css('display','block');
				$("#enrollLimitGroup").css('display','none');
				$("#remarksGroup").css('display','none');
				$("#titleGroup").css('display','none');
				
				$("#interactStartDateGroup").css('display','block');
				$("#interactEndDateGroup").css('display','block');
				$("#briefGroup").css('display','block');
				$("#descriptionGroup").css('display','block');
				$("#moreImagesGroup").css('display','block');
				
				$("#enrollLimitGroup").css('display','block');
				
				
				
			}if(type == "806"){
				$("#eventFlowListGroup").css('display','none');
				$("#eventLotteryListGroup").css('display','block');
				$("#eventPrizeListGroup").css('display','none');
				$("#interactStartDateGroup").css('display','none');
				$("#interactEndDateGroup").css('display','none');
				$("#enrollLimitGroup").css('display','block');
				
				$("#descriptionGroup").css('display','block');
				
			}
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/market/event/">活动列表</a></li>
		<li class="active"><a href="${ctx}/market/event/form?id=${event.id}">活动<shiro:hasPermission name="market:event:edit">${not empty event.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="market:event:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="event" action="${ctx}/market/event/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">活动类型：</label>
			<div class="controls">
				<form:radiobuttons path="type" items="${fns:getDictList('market_promotion')}" onchange="changetype(this.value);"
				itemLabel="label" itemValue="value" htmlEscape="false" class="required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">活动名称：</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" id="titleGroup">
			<label class="control-label">活动标题：</label>
			<div class="controls">
				<form:input path="title" htmlEscape="false" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">选择渠道：</label>
			<div class="controls">
				<div id="menuTree" class="ztree" style="margin-left:100px;margin-top:3px;float:left;"></div>
				<form:hidden path="channelIds" />
			</div>
		</div>
		<div class="control-group" style="display:none;"  id="Group">
			<label class="control-label">活动副标题：</label>
			<div class="controls">
				<form:input path="subTitle" htmlEscape="false" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group" id="briefGroup"> 
			<label class="control-label">简介：</label>
			<div class="controls">
				<form:textarea path="brief" htmlEscape="false" rows="4" class="input-xxlarge "/>
			</div>
		</div>
		<div class="control-group" id="descriptionGroup">
			<label class="control-label">详情：</label>
			<div class="controls">
				<form:textarea path="description" htmlEscape="false" rows="4" class="input-xxlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">开始日期：</label>
			<div class="controls">
				<input name="eventStartDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate required"
					value="<fmt:formatDate value="${event.eventStartDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">截止日期：</label>
			<div class="controls">
				<input name="eventEndDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate required"
					value="<fmt:formatDate value="${event.eventEndDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group"  style="display:none;"  id="interactStartDateGroup">
			<label class="control-label">参与开始：</label>
			<div class="controls">
				<input name="interactStartDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
					value="<fmt:formatDate value="${event.interactStartDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
			</div>
		</div>
		<div class="control-group" style="display:none;"  id="interactEndDateGroup">
			<label class="control-label">参与截止：</label>
			<div class="controls">
				<input name="interactEndDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
					value="<fmt:formatDate value="${event.interactEndDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">地址：</label>
			<div class="controls">
				<form:input path="address" htmlEscape="false" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">封面：</label>
			<div class="controls">
				<form:hidden id="coverImage" path="coverImage" htmlEscape="false" maxlength="200" class="input-xlarge"/>
				<sys:ckfinder input="coverImage" type="files" uploadPath="/hqhsc" selectMultiple="true"/>
			</div>
		</div>
		<div class="control-group"  style="display:none;"  id="moreImagesGroup">
			<label class="control-label">详情图片：</label>
			<div class="controls">
				<form:hidden id="moreImages" path="moreImages" htmlEscape="false" maxlength="200" class="input-xlarge"/>
				<sys:ckfinder input="moreImages" type="files" uploadPath="/hqhsc" selectMultiple="true"/>
			</div>
		</div>
		<div class="control-group" style="display:none;"  id="enrollLimitGroup">
			<label class="control-label">每个可参与次数：</label>
			<div class="controls">
				<form:input path="enrollLimit" htmlEscape="false" maxlength="11" class="input-xlarge  digits"/>
			</div>
		</div>
		<div class="control-group" style="display:none;"  id="remarksGroup">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" class="input-xxlarge"/>
			</div>
		</div>
			<div class="control-group" style="display:block;"  id="eventFlowListGroup">
				<label class="control-label">活动流程：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>步骤编号</th>
								<th>开始日期</th>
								<th>截止日期</th>
								<th>内容</th>
								<th>备注</th>
								<shiro:hasPermission name="market:event:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="eventFlowList">
						</tbody>
						<shiro:hasPermission name="market:event:edit"><tfoot>
							<tr><td colspan="7"><a href="javascript:" onclick="addRow('#eventFlowList', eventFlowRowIdx, eventFlowTpl);eventFlowRowIdx = eventFlowRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission>
					</table>
					<script type="text/template" id="eventFlowTpl">//<!--
						<tr id="eventFlowList{{idx}}">
							<td class="hide">
								<input id="eventFlowList{{idx}}_id" name="eventFlowList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="eventFlowList{{idx}}_delFlag" name="eventFlowList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="eventFlowList{{idx}}_rank" name="eventFlowList[{{idx}}].rank" type="text" value="{{row.rank}}" maxlength="45" class="input-small  digits"/>
							</td>
							<td>
								<input id="eventFlowList{{idx}}_startDate" name="eventFlowList[{{idx}}].startDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
									value="{{row.startDate}}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
							</td>
							<td>
								<input id="eventFlowList{{idx}}_endDate" name="eventFlowList[{{idx}}].endDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
									value="{{row.endDate}}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
							</td>
							<td>
								<input id="eventFlowList{{idx}}_content" name="eventFlowList[{{idx}}].content" type="text" value="{{row.content}}" maxlength="45" class="input-small "/>
							</td>
							<td>
								<input id="eventFlowList{{idx}}_note" name="eventFlowList[{{idx}}].note" type="text" value="{{row.note}}" class="input-small "/>
							</td>
							<shiro:hasPermission name="market:event:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#eventFlowList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var eventFlowRowIdx = 0, eventFlowTpl = $("#eventFlowTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(event.eventFlowList)};
							for (var i=0; i<data.length; i++){
								addRow('#eventFlowList', eventFlowRowIdx, eventFlowTpl, data[i]);
								eventFlowRowIdx = eventFlowRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
			<div class="control-group" style="display:none;"  id="eventLotteryListGroup">
				<label class="control-label">抽奖的奖品设置：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>奖品名称</th>
								<th>奖品总数</th>
								<th>概率比</th>
								<th>封面</th>
								<th>剩下的奖品数量</th>
								<shiro:hasPermission name="market:event:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="eventLotteryList">
						</tbody>
						<shiro:hasPermission name="market:event:edit"><tfoot>
							<tr><td colspan="8"><a href="javascript:" onclick="addRow('#eventLotteryList', eventLotteryRowIdx, eventLotteryTpl);eventLotteryRowIdx = eventLotteryRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission>
					</table>
					<script type="text/template" id="eventLotteryTpl">//<!--
						<tr id="eventLotteryList{{idx}}">
							<td class="hide">
								<input id="eventLotteryList{{idx}}_id" name="eventLotteryList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="eventLotteryList{{idx}}_delFlag" name="eventLotteryList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="eventLotteryList{{idx}}_name" name="eventLotteryList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="100" class="input-small required"/>
							</td>
							<td>
								<input id="eventLotteryList{{idx}}_quantity" name="eventLotteryList[{{idx}}].quantity" type="text" value="{{row.quantity}}" maxlength="45" class="input-small required"/>
							</td>
							<td>
								<input id="eventLotteryList{{idx}}_weight" name="eventLotteryList[{{idx}}].weight" type="text" value="{{row.weight}}" maxlength="11" class="input-small required number"/>
							</td>
							<td>
								<input id="eventLotteryList{{idx}}_image" name="eventLotteryList[{{idx}}].image" type="hidden" value="{{row.image}}" maxlength="200"/>
								<sys:ckfinder input="eventLotteryList{{idx}}_image" type="files" uploadPath="/hqhsc" selectMultiple="true"/>
							</td>
							<td>
								<input id="eventLotteryList{{idx}}_quantityLeft" name="eventLotteryList[{{idx}}].quantityLeft" type="text" value="{{row.quantityLeft}}" maxlength="11" class="input-small "/>
							</td>
							<shiro:hasPermission name="market:event:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#eventLotteryList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var eventLotteryRowIdx = 0, eventLotteryTpl = $("#eventLotteryTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(event.eventLotteryList)};
							for (var i=0; i<data.length; i++){
								addRow('#eventLotteryList', eventLotteryRowIdx, eventLotteryTpl, data[i]);
								eventLotteryRowIdx = eventLotteryRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
			<div class="control-group" style="display:none;"  id="eventPrizeListGroup">
				<label class="control-label">奖品设置：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>奖品编号</th>
								<th>奖品名称</th>
								<th>数量</th>
								<th>奖金</th>
								<th>图片</th>
								<shiro:hasPermission name="market:event:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="eventPrizeList">
						</tbody>
						<shiro:hasPermission name="market:event:edit"><tfoot>
							<tr><td colspan="10"><a href="javascript:" onclick="addRow('#eventPrizeList', eventPrizeRowIdx, eventPrizeTpl);eventPrizeRowIdx = eventPrizeRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission>
					</table>
					<script type="text/template" id="eventPrizeTpl">//<!--
						<tr id="eventPrizeList{{idx}}">
							<td class="hide">
								<input id="eventPrizeList{{idx}}_id" name="eventPrizeList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="eventPrizeList{{idx}}_delFlag" name="eventPrizeList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="eventPrizeList{{idx}}_rank" name="eventPrizeList[{{idx}}].rank" type="text" value="{{row.rank}}" maxlength="45" class="input-small "/>
							</td>
							<td>
								<input id="eventPrizeList{{idx}}_name" name="eventPrizeList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="45" class="input-small "/>
							</td>
							<td>
								<input id="eventPrizeList{{idx}}_quantity" name="eventPrizeList[{{idx}}].quantity" type="text" value="{{row.quantity}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="eventPrizeList{{idx}}_bonus" name="eventPrizeList[{{idx}}].bonus" type="text" value="{{row.bonus}}" class="input-small "/>
							</td>
							<td>
								<input id="eventPrizeList{{idx}}_image" name="eventPrizeList[{{idx}}].image" type="hidden" value="{{row.image}}" maxlength="200"/>
								<sys:ckfinder input="eventPrizeList{{idx}}_image" type="files" uploadPath="/hqhsc" selectMultiple="true"/>
							</td>
							<shiro:hasPermission name="market:event:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#eventPrizeList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var eventPrizeRowIdx = 0, eventPrizeTpl = $("#eventPrizeTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(event.eventPrizeList)};
							for (var i=0; i<data.length; i++){
								addRow('#eventPrizeList', eventPrizeRowIdx, eventPrizeTpl, data[i]);
								eventPrizeRowIdx = eventPrizeRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
		<div class="form-actions">
			<shiro:hasPermission name="market:event:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>